package com.cspg.ssss.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.domain.CheckBatchInst;
import com.cspg.ssss.domain.DwdggbmjdDataEntity;
import com.cspg.ssss.domain.DwdggbmjdTemporaryEntity;
import com.cspg.ssss.service.*;
import com.cspg.ssss.util.DateUtil;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ppp")
public class ppp {
    @Autowired
    CheckBatchService checkBatchService;
    @Autowired
    SelectWenTiService selectWenTiService;
    @Autowired
    ProblemGradeService problemGradeService;
    @Autowired
    DwdggbmjdTemporaryService dwdggbmjdTemporaryService;
    @Autowired
    DwdggbmjdDataService dwdggbmjdDataService;
    @Autowired
    WxSendMsgService wxSendMsgService;

    /**
     * 评分手动启动批次方法
     */
    @RequestMapping(value = "/gsgrade",method = RequestMethod.GET)
    public ResultVo gsgrade() {
        ResultVo retVo=null;
        //根据配置启动批次
        CheckBatchInst inst = new CheckBatchInst();
        inst.setCheck_type(0);
        inst.setStart_date(DateUtil.getCurrentDate());
       int c=checkBatchService.createCheckBatchInstsd(inst);
       if(c==0){
           retVo = ResultVoUtil.success("公司评分手动开启成功","" ,"400");
       }else if(c==-2){
           retVo = ResultVoUtil.error("200","公司评分手动开启失败，已有启动的公司评分批次" ,"");
       }else{
           retVo = ResultVoUtil.error("9999","出现未知异常" ,"");
       }
//        inst.setCheck_type(1);
//        checkBatchService.createCheckBatchInst(inst);

        //切换阶段
       /* problemGradeService.setNextStage(0);
        problemGradeService.setNextStage(1);*/
         return retVo;
    }
    @RequestMapping(value = "/dwgrade",method = RequestMethod.GET)
    public ResultVo dwgrade() {
        ResultVo retVo=null;
        //根据配置启动批次
        CheckBatchInst inst = new CheckBatchInst();
        inst.setCheck_type(1);
        inst.setStart_date(DateUtil.getCurrentDate());
        int c=checkBatchService.createCheckBatchInstsd(inst);
        if(c==0){
            retVo = ResultVoUtil.success("单位评分手动开启成功","" ,"400");
        }else if(c==-2){
            retVo = ResultVoUtil.error("200","单位评分手动开启失败，已有启动的单位评分批次" ,"");
        }else{
            retVo = ResultVoUtil.error("9999","出现未知异常" ,"");
        }
//        inst.setCheck_type(1);
//        checkBatchService.createCheckBatchInst(inst);

        //切换阶段
       /* problemGradeService.setNextStage(0);
        problemGradeService.setNextStage(1);*/
        return retVo;
    }

    /**
     * 企业微信消息推送
     * @param
     */
    @RequestMapping(value = "/send",method = RequestMethod.GET)
    public void sendTest() {
        wxSendMsgService.sendTextMsg2("lujiaming","","好好看好好学","这是企业微信消息发送");
    }

    /**
     * 导入整改结果测试方法
     * @version 1.0
     * @param jSONArray
     * @return String
     */
    @RequestMapping(value = "/addMore", method = RequestMethod.POST)
    public ResultVo AddMore(@RequestBody JSONArray jSONArray) {
        ResultVo retVo=null;
        JSONObject retObj = new JSONObject();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");


        List<String> list4=new ArrayList<>();
        List zhenggailist=new ArrayList<>();
        List cuowu=new ArrayList<>();
        Map map=new HashMap<>();
        DwdggbmjdDataEntity dwdggbmjdDataEntity;
        for(int i=0;i<jSONArray.size();i++) {
            int count=dwdggbmjdDataService.selectDataCount();//拿到单位表总数量
            JSONObject list=jSONArray.getJSONObject(i);// 获取对象
            String dms="超高压输电公司/广州局/安全监管部";//邓导入
            String ljm="超高压输电公司";//卢导入
            String a=list.get("wtfxfs").toString()+list.get("dwmc").toString()+dms+
                    list.get("wtms").toString()+list.get("wtfxsj").toString();       //邓导入的数据提单人单位是 超高压输电公司/广州局/安全监管部
            String dmsid = DigestUtils.md5DigestAsHex(a.getBytes());
            DwdggbmjdDataEntity dwdggbmjdDataEntityD=dwdggbmjdDataService.selectDataId(dmsid);//查询邓导入的是否存在

            String s=list.get("wtfxfs").toString()+list.get("dwmc").toString()+ljm+
                    list.get("wtms").toString()+list.get("wtfxsj").toString();       //卢导入的数据提单人单位是 超高压输电公司
            String ljmid = DigestUtils.md5DigestAsHex(s.getBytes());
            DwdggbmjdDataEntity dwdggbmjdDataEntityL=dwdggbmjdDataService.selectDataId(ljmid);//查询卢导入的是否存在

            //使用问题发现方式，单位名称，部门名称，问题描述，问题发现时间进行字符串组合
            String c=list.get("wtfxfs").toString()+list.get("dwmc").toString()+list.get("departmentname").toString()+
                    list.get("wtms").toString()+list.get("wtfxsj").toString();     //新数据无提单人单位字段
            //加密生成唯一id
            String id = DigestUtils.md5DigestAsHex(c.getBytes());
            Date d = new Date();
            String b=sdf.format(d);
            Date now= null;
            try {
                now = sdf.parse(b);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //判断当前数据是否卢或者邓导入
            if(dwdggbmjdDataEntityD!=null || dwdggbmjdDataEntityL!=null){
                String refid="";
                if(dwdggbmjdDataEntityD!=null){//如果是邓导入
                    refid=dwdggbmjdDataEntityD.getId();
                }else{                    //如果是卢导入
                    refid=dwdggbmjdDataEntityL.getId();
                }
                System.out.println(refid);
                //查询单位问题完成表
                List<Map> dwretify=selectWenTiService.selectrectify(refid);
                if(dwretify.size()>0){
                    System.out.println("邓/卢导入的数据，整改有记录");
                    //如果有记录就修改
                    for (int j=0;j<dwretify.size();j++) {
                        String bmmc=list.get("bmmc").toString().replaceAll("->","/" );
                        map.put("problem_id", dwretify.get(j).get("problem_id").toString());//id
                        map.put("duty_dept","超高压输电公司/"+bmmc);//责任部门路径
                        map.put("duty_date", list.get("zgqx").toString());//指定责任部门时间
                        map.put("rectify_content", list.get("zgcs").toString());//整改措施
                        map.put("rectify_deadline", list.get("zgqx"));//整改期限
                        map.put("rectify_commit", list.get("wcqk").toString());//整改情况
                        map.put("rectify_commit_date", list.get("zgwcsj"));//落实时间
                        map.put("complete_date", list.get("zgwcsj"));//验收时间
                        map.put("rectify_status", "complete");//整改状态
                        map.put("problem_type", "1");//flow_type
                        map.put("flow_type", "1");
                    }
                   int dw=selectWenTiService.updaterectify(map);
                    System.out.println(dw);
                }else{
                    System.out.println("邓/卢导入的数据，整改无记录");
                    String bmmc=list.get("bmmc").toString().replaceAll("->","/" );
                    map.put("problem_id",refid);//id
                    map.put("duty_dept","超高压输电公司/"+bmmc);//责任部门路径
                    map.put("duty_date",list.get("zgqx"));//指定责任部门时间
                    map.put("rectify_content",list.get("zgcs").toString());//整改措施
                    map.put("rectify_deadline",list.get("zgqx"));//整改期限
                    map.put("rectify_commit",list.get("wcqk").toString());//整改情况
                    map.put("rectify_commit_date",list.get("zgwcsj"));//落实时间
                    map.put("complete_date",list.get("zgwcsj"));//验收时间
                    map.put("rectify_status","complete");//整改状态
                    map.put("problem_type","1");//
                    map.put("flow_type", "1");
                    //如果没有记录就新增
                    int dw=selectWenTiService.insertrectify(map);
                    System.out.println(dw);
                }

            }else {   //如果不是邓或者卢导入的数据
                System.out.println("不是邓或者卢导入的数据");
                dwdggbmjdDataEntity = dwdggbmjdDataService.selectDataId(id);//查询是否存在
                System.out.println(id);
                list.put("id", id);
                if (dwdggbmjdDataEntity == null) {
                    System.out.println("不是邓或者卢导入的数据,问题无记录，整改无记录");
                    count=count+1; //序号等于单位表总数加1
                    String cc=String.valueOf(count);
                   DwdggbmjdDataEntity dwdx=new DwdggbmjdDataEntity();
                    dwdx=JSONObject.toJavaObject(list, DwdggbmjdDataEntity.class);
                    dwdx.setXh(cc);
                    dwdx.setDrtime(now);
                    dwdx.setDrman("卢家明");
                    dwdggbmjdDataService.insert(dwdx);
                    //给新增的问题增加整改记录
                    String bmmc=list.get("bmmc").toString().replaceAll("->","/" );
                    map.put("problem_id",dwdx.getId());//id
                    map.put("duty_dept","超高压输电公司/"+bmmc);//责任部门路径
                    map.put("duty_date",list.get("zgqx"));//指定责任部门时间
                    map.put("rectify_content",list.get("zgcs").toString());//整改措施
                    map.put("rectify_deadline",list.get("zgqx"));//整改期限
                    map.put("rectify_commit",list.get("wcqk").toString());//整改情况
                    map.put("rectify_commit_date",list.get("zgwcsj"));//落实时间
                    map.put("complete_date",list.get("zgwcsj"));//验收时间
                    map.put("rectify_status","complete");//整改状态
                    map.put("problem_type","1");//
                    map.put("flow_type", "1");
                    //如果没有记录就新增
                    selectWenTiService.insertrectify(map);

                }else{
                    System.out.println("不是邓或者卢导入的数据,问题有记录");
                    List<Map> dwretify=selectWenTiService.selectrectify(dwdggbmjdDataEntity.getId());
                    if(dwretify.size()>0){
                        System.out.println("不是邓或者卢导入的数据，整改有记录，修改记录");
                        //如果有记录就修改
                        for (int j=0;j<dwretify.size();j++) {
                            String bmmc=list.get("bmmc").toString().replaceAll("->","/" );
                            map.put("problem_id", dwretify.get(j).get("problem_id").toString());//id
                            map.put("duty_dept","超高压输电公司/"+bmmc);//责任部门路径
                            map.put("duty_date", list.get("zgqx").toString());//指定责任部门时间
                            map.put("rectify_content", list.get("zgcs").toString());//整改措施
                            map.put("rectify_deadline", list.get("zgqx"));//整改期限
                            map.put("rectify_commit", list.get("wcqk").toString());//整改情况
                            map.put("rectify_commit_date", list.get("zgwcsj"));//落实时间
                            map.put("complete_date", list.get("zgwcsj"));//验收时间
                            map.put("rectify_status", "complete");//整改状态
                            map.put("problem_type", "1");//flow_type
                            map.put("flow_type", "1");
                        }
                        int dw=selectWenTiService.updaterectify(map);
                        System.out.println(dw);
                    }else{
                        System.out.println("不是邓或者卢导入的数据，整改无记录，新增记录");
                        String bmmc=list.get("bmmc").toString().replaceAll("->","/" );
                        map.put("problem_id",dwdggbmjdDataEntity.getId());//id
                        map.put("duty_dept","超高压输电公司/"+bmmc);//责任部门路径
                        map.put("duty_date",list.get("zgqx"));//指定责任部门时间
                        map.put("rectify_content",list.get("zgcs").toString());//整改措施
                        map.put("rectify_deadline",list.get("zgqx"));//整改期限
                        map.put("rectify_commit",list.get("wcqk").toString());//整改情况
                        map.put("rectify_commit_date",list.get("zgwcsj"));//落实时间
                        map.put("complete_date",list.get("zgwcsj"));//验收时间
                        map.put("rectify_status","complete");//整改状态
                        map.put("problem_type","1");//
                        map.put("flow_type", "1");
                        //如果没有记录就新增
                        int dw=selectWenTiService.insertrectify(map);
                        System.out.println(dw);
                    }
                }
            }

        }










 /*       if(cuowu.size()>0){
            retVo = ResultVoUtil.error("0000","有问题无法对应",cuowu);
            return  retVo;
        }
        int cc= selectWenTiService.bacthinsert(zhenggailist);
        if (cc> 0) {
            retVo = ResultVoUtil.success("新增成功","" ,"400");
        }else{
            retVo = ResultVoUtil.error("0000","插入失败","");
        }*/

        return retVo;
    }
}
